﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsistentHashing
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 100万kv数据，10个物理节点缓存分布测试
            ////初始化节点数据，10个物理节点
            //List<string> nodeNames = new List<string>();
            //nodeNames.Add("192.168.1.110");
            //nodeNames.Add("192.168.1.109");
            //nodeNames.Add("192.168.1.108");
            //nodeNames.Add("192.168.1.107");
            //nodeNames.Add("192.168.1.106");
            //nodeNames.Add("192.168.1.105");
            //nodeNames.Add("192.168.1.104");
            //nodeNames.Add("192.168.1.103");
            //nodeNames.Add("192.168.1.102");
            //nodeNames.Add("192.168.1.101");

            //ConsistentHashing cHash = new ConsistentHashing(nodeNames);
            //string cacheVN = "virtual";
            //SortedList<string, int> nodeCounts = new SortedList<string, int>();

            ////100万KV数据
            //const int TOTALNUMBER = 10000;
            //for (int k = 0; k < TOTALNUMBER; k++)
            //{
            //    //模拟数据
            //    string cache = cacheVN + "&Data" + k.ToString();
            //    int hash = ConsistentHashing.HashAlgorithm(cache);
            //    string node = cHash.GetNodeName(hash);
            //    //计数
            //    if (nodeCounts.ContainsKey(node))
            //    {
            //        nodeCounts[node]++;
            //    }
            //    else
            //    {
            //        nodeCounts[node] = 1;
            //    }
            //}

            ////输出统计结果
            //foreach (var col in nodeCounts)
            //{
            //    double d = col.Value * 100.0 / TOTALNUMBER;
            //    Console.WriteLine("NodeName[" + col.Key + "],NodeCount[" + col.Value.ToString() + "],Rate[" + d.ToString("0.00") + "%]");
            //}

            #endregion

            #region 使用StackExchage.Redis模块的测试
            //Node node = new Node();
            //node.StringSet();
            //var abb = node.StringGet("121");
            //Console.WriteLine("Get,key[121], value["+abb+"]");
            //node.MasterServerInfo();
            #endregion

            #region 使用一致性hash调用redis集群
            //初始化节点数据，10个物理节点
            List<string> nodeNames = new List<string>();
            nodeNames.Add("127.0.0.1:6381");
            nodeNames.Add("127.0.0.1:6388");
            Dictionary<string, string> nodeConfigsDic = new Dictionary<string, string>();
            nodeConfigsDic.Add("127.0.0.1:6381", "localhost:20001,serviceName=mymaster,allowAdmin=true");
            nodeConfigsDic.Add("127.0.0.1:6388", "localhost:20002,serviceName=mymaster1,allowAdmin=true");
            ConsistentHashing cHash = new ConsistentHashing(nodeNames);
            cHash.SetNodeConfigs(nodeConfigsDic);
            cHash.InitNodes();

            string cacheVN = "abcVN";
            string cacheValue = "ddd";

            //写缓存
            int hash = ConsistentHashing.HashAlgorithm(cacheVN);
            Node node = cHash.GetNode(hash);
            node.StringSet(cacheVN, cacheValue);

            //读缓存
            hash = ConsistentHashing.HashAlgorithm(cacheVN);
            node = cHash.GetNode(hash);

            string rv = node.StringGet(cacheVN);
            Console.WriteLine("key:["+ cacheVN + "], value:["+rv +"]");
            #endregion
            Console.ReadLine();
        }
    }
}
